package com.zb.kc.order.controller;


import com.zb.kc.commonutils.R;
import com.zb.kc.commonutils.ResultCode;
import com.zb.kc.order.service.PayLogService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author wanggen
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/eduorder/paylog")
@Slf4j
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    // 生成微信支付二维码接口
    // 参数是订单号
    @ApiOperation(value = "生成微信支付二维码接口")
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        // 返回信息，包含二维码地址，还有其他需要的信息
        Map<String, String> map = payLogService.createNative(orderNo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "查询订单支付状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        if (map == null) {
            // 支付失败
            return R.err(ResultCode.PAY_ERROR);
        }
        if (map.get("trade_state").equals("SUCCESS")) {
            // 添加记录到支付表，更新订单表订单状态
            payLogService.updateOrderStatus(map);
            return R.ok();
        }
        // 支付中
        return R.err(ResultCode.PAYING);
    }

}
